package com.ysm.smfu.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysm.smfu.R;

public class SView extends RelativeLayout {

    private EditText et;
    private TextView tv;

    public SView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.sv, null);
        et = (EditText) view.findViewById(R.id.et);
        tv = (TextView) view.findViewById(R.id.tv);
        addView(view);
        setListener();
    }

    private void setListener() {

        et.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                et.setFocusable(true);
                et.setFocusableInTouchMode(true);
                et.requestFocus();
            }
        });

        et.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    TranslateAnimation animation = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, -1f,
                            Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 0);
                    AlphaAnimation animation2 = new AlphaAnimation(1f, 0);
                    AnimationSet animationSet = new AnimationSet(false);

                    animationSet.addAnimation(animation);
                    animationSet.addAnimation(animation2);
                    animationSet.setDuration(300);
                    animationSet.setFillAfter(true);
                    tv.setAnimation(animationSet);
                    tv.setVisibility(GONE);
                }
//				else{
//
//                    TranslateAnimation animation = new TranslateAnimation(
//							Animation.RELATIVE_TO_SELF, -1f,
//							Animation.RELATIVE_TO_SELF, 0f,
//							Animation.RELATIVE_TO_SELF, 0,
//							Animation.RELATIVE_TO_SELF, 0);
//					AlphaAnimation animation2 = new AlphaAnimation(0f, 1f);
//					AnimationSet animationSet = new AnimationSet(false);
//
//					animationSet.addAnimation(animation);
//					animationSet.addAnimation(animation2);
//					animationSet.setDuration(300);
//					animationSet.setFillAfter(true);
//					tv.setAnimation(animationSet);
//				}
            }
        });
    }

    public SView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public String getText() {
        return et.getText().toString().trim();
    }

}
